<?php

/**
 * @version     $Id: autoban.php 3 2010-10-08 17:49:35Z obalyuk87@gmail.com $
 * @package     AdminSafe
 * @subpackage  Components
 * @copyright   Copyright (C) 2010 Oleksandr Balyuk (www.boolcast.com)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL v2
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
// No direct access
defined('_JEXEC') or die('Restricted access');

/**
 * Description of autobancounter
 *
 * @author Oleksandr Balyuk <obalyuk@kahzam.com>
 */
class TableAutoban extends JTable {

    public $ip_address = null;
    public $hits = null;

    /**
     * Default Constructor
     *
     * @param JDatabase Database connector object
     */
    function __construct(&$db) {
        parent::__construct('#__adminsafe_autoban_counter', 'ip_address', $db);
    }

    /**
     * Checks table values
     *
     * @return bool
     */
    public function check() {

        //
        if (!$this->ip_address) {
            $this->setError(JText::_('Invalid IP Address'));
            return false;
        }

        //  Clean up IP Address
        $this->ip_address = trim($this->ip_address);

        //
        return parent::check();
    }

    /**
     * Store the IP address
     *
     * @param bool $updateNulls
     * @return bool
     */
    public function store($updateNulls = false) {

        $ret = false;

        //
        $ret = $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key);

        //
        if (!$ret) {
            $this->setError(get_class($this) . '::store failed - ' . $this->_db->getErrorMsg());
            return false;
        } else {
            return true;
        }
    }

    /**
     * Default delete method
     *
     * can be overloaded/supplemented by the child class
     *
     * @access public
     * @return true if successful otherwise returns and error message
     */
    function delete($oid=null) {

        $k = $this->_tbl_key;
        if ($oid) {
            $this->$k = $oid;
        }

        $query = 'DELETE FROM ' . $this->_db->nameQuote($this->_tbl) .
                ' WHERE ' . $this->_tbl_key . ' = ' . $this->_db->Quote($this->$k);
        $this->_db->setQuery($query);

        if ($this->_db->query()) {
            return true;
        } else {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
    }

    /**
     * Description
     *
     * @access public
     * @param $oid
     * @param $log
     */
    function hit($oid=null, $log=false) {
       
        $k = $this->_tbl_key;

        if ($oid !== null) {
            $this->$k = $oid;
        }

        $query = 'UPDATE ' . $this->_tbl
                . ' SET hits = ( hits + 1 )'
                . ' WHERE ' . $this->_tbl_key . '=' . $this->_db->Quote($this->$k);
        $this->_db->setQuery($query);
        $this->_db->query();
        $this->hits++;
    }

}